Conditions | 1 |
Paths | 1 |
Total Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | const fs = require('fs') |
||
7 | describe('Output helper', () => { |
||
8 | const spyError = jest.spyOn(console, 'error').mockImplementation(() => {}) |
||
|
|||
9 | const spyLog = jest.spyOn(console, 'log').mockImplementation(() => {}) |
||
10 | |||
11 | const file = 'src/test/output.txt' |
||
12 | const path = join(file) |
||
13 | |||
14 | it('output has empty data', () => { |
||
15 | const result = outputHelper('', null) |
||
16 | expect(result).toBe(null) |
||
17 | }) |
||
18 | |||
19 | it('output is console', () => { |
||
20 | outputHelper(null, 'data') |
||
21 | expect(spyLog).toHaveBeenCalled() |
||
22 | expect(spyLog.mock.calls[0][0]).toContain('data') |
||
23 | }) |
||
24 | |||
25 | it('output to file', done => { |
||
26 | fs.truncateSync(path) |
||
27 | outputHelper(file, 'data') |
||
28 | setTimeout(() => { |
||
29 | try { |
||
30 | expect(fs.readFileSync(path, 'utf8')).toBe('data\r\n') |
||
31 | done() |
||
32 | } catch (err) { |
||
33 | done.fail(err) |
||
34 | } |
||
35 | }, TIMEOUT) |
||
36 | }) |
||
37 | |||
38 | it('output to not existed file', done => { |
||
39 | const dummy = 'dummy.txt' |
||
40 | outputHelper(dummy, 'data') |
||
41 | setTimeout(() => { |
||
42 | try { |
||
43 | expect(spyError).toHaveBeenCalled() |
||
44 | expect(spyError.mock.calls[0][0]).toContain('Cannot write file to') |
||
45 | done() |
||
46 | } catch (err) { |
||
47 | done.fail(err) |
||
48 | } |
||
49 | }, TIMEOUT) |
||
50 | }) |
||
51 | |||
52 | it('output to write stream', done => { |
||
53 | const writeable = fs.createWriteStream(path) |
||
54 | outputHelper(writeable, 'data_stream') |
||
55 | setTimeout(() => { |
||
56 | try { |
||
57 | expect(fs.readFileSync(path, 'utf8')).toBe('data_stream\r\n') |
||
58 | done() |
||
59 | } catch (err) { |
||
60 | done.fail(err) |
||
61 | } |
||
62 | }, TIMEOUT) |
||
63 | }) |
||
64 | }) |
||
65 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.